package com.zhbit.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.zhbit.dao.UserDaoImpl;
import com.zhbit.domain.User;
import com.zhbit.service.UserService;
import com.zhbit.service.UserServiceImpl;


public class UserAction extends ActionSupport {
	private String username;
	private String password;
	private Map<String,Object> request; 
	@Resource
	private UserService userService;
//	private Map<String,Object> session; 
//	public void setSession(Map<String,Object> session){
//		this.session=session;
//	}
//	private User user;
//	private String repassword;
//	
//	
//	public String getRepassword() {
//		return repassword;
//	}
//	public void setRepassword(String repassword) {
//		this.repassword = repassword;
//	}
//	public User getUser() {
//		return user;
//	}
//	public void setUser(User user) {
//		this.user = user;
//	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String register() {
		return "register_success";
	}
	
	
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public String Login(){
		//UserDaoImpl userDaoImpl = new UserDaoImpl();
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		//userService = new UserServiceImpl();
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(userService.Login(user)){
			System.out.println(user.getUsername());
			session.setAttribute("loginInfo", user.getUsername());
			//session.put("loginInfo", user.getUsername());
			//ActionContext.getContext().getSession().put("user", user.getUsername());
			//request.put(username, password);
			//ActionContext ac = ActionContext.getContext();
			//ac.getApplication().put("uname", username);
			//request.getSession().setAttribute("Name",username);
			return "success";
		}else{
			session.setAttribute("loginError", "用户名或者密码错误!");
			return "error";
		}
			
	}
	public void setRequest(Map<String,Object> request){
		this.request = request;
	} 
}
